package com.degon.hsyl.choosefile.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.degon.hsyl.choosefile.R;
import com.degon.hsyl.choosefile.adapter.ChooseFileCallBack;
import com.degon.hsyl.choosefile.adapter.FileListAdapter;
import com.degon.hsyl.choosefile.bean.FileBean;
import com.degon.hsyl.choosefile.util.GetFileUtil;

import java.util.ArrayList;
import java.util.List;

public class PptListFragment extends Fragment {

    private RecyclerView pptFileList;
    private List<FileBean> fileList = new ArrayList<>();
    private FileListAdapter adapter;
    private LinearLayout mainView;
    private ImageView noResult;
    private ChooseFileCallBack callBack;

    public PptListFragment(ChooseFileCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ppt_list, container, false);

        List<String> fileTypes = new ArrayList<>();
        fileTypes.add("ppt");
        fileTypes.add("pptx");

        pptFileList = view.findViewById(R.id.ppt_file_list);
        mainView = view.findViewById(R.id.doc_file_list_bg_3);
        noResult = view.findViewById(R.id.no_result_3);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        pptFileList.setLayoutManager(manager);
        fileList = GetFileUtil.searchByType(requireContext(), fileTypes);

        if (fileList.size() == 0) {
            mainView.setVisibility(View.GONE);
            noResult.setVisibility(View.VISIBLE);
        } else {
            adapter = new FileListAdapter(fileList);
            pptFileList.setAdapter(adapter);

            adapter.setOnItemClickListener(new FileListAdapter.OnItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    CheckBox checkBox = view.findViewById(R.id.choose_file);
                    RelativeLayout chosenFileBg = getActivity().findViewById(R.id.chosen_files_count_3_bg);
                    TextView chosenFileTxt = getActivity().findViewById(R.id.chosen_files_3_count_txt);
                    int chosenFileCount = Integer.parseInt(chosenFileTxt.getText().toString().trim());
                    if (checkBox.isChecked()) {//取消选择
                        checkBox.setChecked(false);
                        chosenFileCount -= 1;
                        if (chosenFileCount == 0) {
                            chosenFileBg.setVisibility(View.GONE);
                            chosenFileTxt.setText("0");
                        }
                        callBack.removeFile(fileList.get(position));
                    } else {//选择文件
                        checkBox.setChecked(true);
                        chosenFileCount += 1;
                        if (chosenFileBg.getVisibility() == View.GONE) {
                            chosenFileBg.setVisibility(View.VISIBLE);
                        }
                        callBack.addFile(fileList.get(position));
                    }
                    chosenFileTxt.setText(String.valueOf(chosenFileCount));
                }

                @Override
                public void onLongClick(View view, int position) {

                }
            });
        }


        return view;
    }
}